home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12538 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  97 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Please help: need to read lines in text file
  5. Date: 1 Apr 1996 07:56:43 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4joubrINNrdl@keats.ugrad.cs.ubc.ca>
  8. References: <315fe5d1.1121568@news.mixcom.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <315fe5d1.1121568@news.mixcom.com>,
  12. Mike McWhinney <elja.inc@mixcom.com> wrote:
  13. >Hello all,
  14. >
  15. >I am not an especially fluent C programmer, but need a short function
  16. >to read the Nth line from a given ASCII/text file.  The target
  17. >platform is MS-DOS, and the compiler I have is Turbo C++, but
  18. >shouldn't matter if it is portable C code.
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22.  
  23. #define INITIAL_BUFSIZE        80
  24. #define BUFSIZE_INCREM        40
  25.  
  26. char *readline(char *filename, int n)
  27.  
  28. {
  29.     FILE *stream = fopen(filename,"r");
  30.     int currline = 1;
  31.     int buffersize = INITIAL_BUFSIZE;
  32.     int charcount = 0;
  33.     char *buffer, *newbuffer;
  34.     int nextchar;
  35.  
  36.     if (!stream)
  37.         goto errquit;
  38.  
  39.     buffer = malloc(buffersize);
  40.  
  41.     while (currline < n) {
  42.         if((nextchar = getc(stream)) == '\n') {
  43.             currline++;
  44.             continue;
  45.         } else if (nextchar == EOF) {
  46.             fclose(stream);
  47.             goto ferrquit;
  48.         }
  49.     }
  50.         
  51.     while(1) {
  52.         nextchar = getc(stream);
  53.  
  54.         if (charcount >= buffersize) {
  55.             buffersize += BUFSIZE_INCREM;
  56.             newbuffer = realloc(buffer, buffersize);
  57.             if (!newbuffer) 
  58.                 goto ferrquit;
  59.             buffer = newbuffer;
  60.         }
  61.  
  62.         if (nextchar == EOF || nextchar == '\n') {
  63.             buffer[charcount++] = '\0';
  64.             break;
  65.         } else 
  66.             buffer[charcount++] = nextchar;
  67.     }
  68.     return realloc(buffer, charcount);
  69.  
  70. ferrquit:
  71.     free(buffer);
  72. errquit:
  73.     return NULL;
  74. }
  75.  
  76. int main(int argc, char **argv)
  77.  
  78. {
  79.     char *str;
  80.  
  81.     if (argc < 3)
  82.         exit(EXIT_FAILURE);
  83.  
  84.      str = readline(argv[1], atoi(argv[2]));
  85.  
  86.     if (!str) {
  87.         puts("failure to read line");
  88.         exit(EXIT_FAILURE);
  89.     } else
  90.         puts(str);
  91.  
  92.     return EXIT_SUCCESS;
  93. }
  94.  
  95. -- 
  96.  
  97.